<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Wechat as WechatModel;
use app\common\model\Member as MemberModel;

/**
 * 微信控制器
 * Class Wechat
 * @package app\home\controller
 */
class Wechat extends Base
{
    /**
     * [bindwechat  绑定微信]
     * @return json
     * @author [C。]
     */
    public function bindwechat()
    {
        $param = input('param.');
        $model = new WechatModel();
        $mmodel = new MemberModel();
        try{
            $member = $mmodel->findOne(['token'=>$param['token']],'id,quality');
            $member1 = $model->findOne(['memid'=>$member['id']]);
            if(!empty($member1)){
                return admin_json(-1, '已绑定微信');
            }
            $wechat = $model->findOne(['unionid'=>$param['unionid']]);
            if(!empty($wechat['memid'])){
                return admin_json(-1, '该微信已绑定用户！');
            }
            if(empty($wechat)){
                $param['memid'] = $member['id'];
                $param['addtime'] = time();
                $model->addData($param);
            }else{
                if(empty($wechat['memid'])){
                    $param['memid'] = $member['id'];
                    $param['id'] = $wechat['id'];
                    $model->editData($param);
                }
            }
            add_quality($member['id'],34,$member['quality'],5);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '绑定失败'.$e->getMessage());
        }
        return admin_json(1, '绑定成功');
    }
}